import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

var user = {
  namespaced: true,
  state: {
    token: '',
    name: '',
    avatar: '',
    access: []
  },
  mutations: {
    resetState: (state) => {
      var tempstate = {
        token: getToken(),
        name: '',
        avatar: '',
        access: []
      }
      Object.assign(state, tempstate)
    },
    setToken: (state, token) => {
      state.token = token
    },
    setName: (state, name) => {
      state.name = name
    },
    setAvatar: (state, avatar) => {
      state.avatar = avatar
    },
    setAccess: (state, access) => {
      state.access = access
    }
  },
  actions: {
    // user login
    login({ commit }, proms) {
      var userInfo = proms.loginForm
      var remeberMe = proms.remeberMe
      console.log(proms)
      const { username, password } = userInfo
      return new Promise((resolve, reject) => {
        login({ username: username.trim(), password: password }).then(response => {
          const { data } = response
          commit('setToken', data.token)
          setToken(data.token, remeberMe)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // get user info
    getInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(response => {
          const { data } = response
          if (!data) {
            return reject('Verification failed, please Login again.')
          }

          const { name, avatar, access } = data

          commit('setName', name)
          commit('setAvatar', avatar)
          commit('setAccess', access)
          resolve(data)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // user logout
    logout({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          removeToken() // must remove  token  first
          resetRouter()
          commit('resetState')
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // remove token
    resetToken({ commit }) {
      return new Promise(resolve => {
        removeToken() // must remove  token  first
        commit('resetState')
        resolve()
      })
    }
  }
}

export default user

// const getDefaultState = () => {
//   return {
//     token: getToken(),
//     name: '',
//     avatar: '',
//     access: []
//   }
// }

// const state = getDefaultState()

// const mutations = {
//   resetState: (state) => {
//     Object.assign(state, getDefaultState())
//   },
//   setToken: (state, token) => {
//     state.token = token
//   },
//   setName: (state, name) => {
//     state.name = name
//   },
//   setAvatar: (state, avatar) => {
//     state.avatar = avatar
//   },
//   setAccess: (state, access) => {
//     state.access = access
//   }
// }

// const actions = {
//   // user login
//   login({ commit }, userInfo) {
//     const { username, password } = userInfo
//     return new Promise((resolve, reject) => {
//       login({ username: username.trim(), password: password }).then(response => {
//         const { data } = response
//         commit('setToken', data.token)
//         setToken(data.token)
//         resolve()
//       }).catch(error => {
//         reject(error)
//       })
//     })
//   },

//   // get user info
//   getInfo({ commit, state }) {
//     return new Promise((resolve, reject) => {
//       getInfo(state.token).then(response => {
//         const { data } = response
//         if (!data) {
//           return reject('Verification failed, please Login again.')
//         }

//         const { name, avatar, access } = data

//         commit('setName', name)
//         commit('setAvatar', avatar)
//         commit('setAccess', access)
//         resolve(data)
//       }).catch(error => {
//         reject(error)
//       })
//     })
//   },

//   // user logout
//   logout({ commit, state }) {
//     return new Promise((resolve, reject) => {
//       logout(state.token).then(() => {
//         removeToken() // must remove  token  first
//         resetRouter()
//         commit('resetState')
//         resolve()
//       }).catch(error => {
//         reject(error)
//       })
//     })
//   },

//   // remove token
//   resetToken({ commit }) {
//     return new Promise(resolve => {
//       removeToken() // must remove  token  first
//       commit('resetState')
//       resolve()
//     })
//   }
// }

// export default {
//   namespaced: true,
//   state,
//   mutations,
//   actions
// }

